package com.aiyige.utils.widget;


import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.utils.UIHelper;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class CommonTitleDetailDialog extends DialogFragment {
    Unbinder unbinder;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    String title;
    String message;

    public String getCancelBtnName() {
        return cancelBtnName;
    }

    public void setCancelBtnName(String cancelBtnName) {
        this.cancelBtnName = cancelBtnName;
    }

    public String getOkBtnName() {
        return okBtnName;
    }

    public void setOkBtnName(String okBtnName) {
        this.okBtnName = okBtnName;
    }

    String cancelBtnName;
    String okBtnName;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.tv_detail)
    TextView tvDetail;

    @BindView(R.id.cancelBtn)
    TextView cancelBtn;

    @BindView(R.id.okBtn)
    TextView okBtn;

    @BindView(R.id.v_center_line)
    View vCenterLine;


    public boolean isBackPressEnable() {
        return backPressEnable;
    }

    public CommonTitleDetailDialog setBackPressEnable(boolean backPressEnable) {
        this.backPressEnable = backPressEnable;
        return this;
    }

    private boolean backPressEnable = true;


    public CommonTitleDetailDialog setListener(CommonTitleDetailDialog.Listener listener) {
        this.listener = listener;
        return this;
    }

    Listener listener;

    public CommonTitleDetailDialog() {
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public static CommonTitleDetailDialog newInstance(String title,String message,String cancelBtnName,String okBtnName) {
        CommonTitleDetailDialog fragment = new CommonTitleDetailDialog();
        fragment.setStyle(STYLE_NORMAL, R.style.CommonDialogTheme);
        fragment.setCancelBtnName(cancelBtnName);
        fragment.setOkBtnName(okBtnName);
        fragment.setMessage(message);
        fragment.setTitle(title);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.common_title_detail_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        tvTitle.setText(title);
        tvDetail.setText(message);
        if (TextUtils.isEmpty(cancelBtnName)) {
            cancelBtn.setVisibility(View.GONE);
            vCenterLine.setVisibility(View.GONE);
        } else {
            cancelBtn.setText(cancelBtnName);
        }
        okBtn.setText(okBtnName);
        if (!isBackPressEnable()) {
            this.getDialog().setOnKeyListener(new DialogInterface.OnKeyListener()
            {
                @Override
                public boolean onKey(DialogInterface arg0, int keyCode, KeyEvent arg2) {
                    // TODO Auto-generated method stub
                    if (keyCode == KeyEvent.KEYCODE_BACK) {

                        return true;
                    }else if(keyCode == KeyEvent.KEYCODE_MENU) {

                        return true;
                    }
                    return false;
                }
            });
            this.setCancelable(false);
        }
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Window window = getDialog().getWindow();
        if (window != null) {
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.width = Resources.getSystem().getDisplayMetrics().widthPixels - UIHelper.dip2px(50);
            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(layoutParams);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.okBtn,R.id.cancelBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancelBtn:
                dismiss();
                break;
            case R.id.okBtn:
                if (listener != null) {
                    listener.onConfirmClassed();
                }
                dismiss();
                break;
        }
    }


    public interface Listener {
        void onConfirmClassed();
    }

}
